/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.OutputTest;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.OutputTestRepository;
import me.zhengjie.modules.system.service.OutputTestService;
import me.zhengjie.modules.system.service.dto.OutputTestDto;
import me.zhengjie.modules.system.service.dto.OutputTestQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.OutputTestMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xxq
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
public class OutputTestServiceImpl implements OutputTestService {

    private final OutputTestRepository outputTestRepository;
    private final OutputTestMapper outputTestMapper;

    @Override
    public Map<String,Object> queryAll(OutputTestQueryCriteria criteria, Pageable pageable){
        Page<OutputTest> page = outputTestRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(outputTestMapper::toDto));
    }

    @Override
    public List<OutputTestDto> queryAll(OutputTestQueryCriteria criteria){
        return outputTestMapper.toDto(outputTestRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OutputTestDto findById(Integer id) {
        OutputTest outputTest = outputTestRepository.findById(id).orElseGet(OutputTest::new);
        ValidationUtil.isNull(outputTest.getId(),"OutputTest","id",id);
        return outputTestMapper.toDto(outputTest);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OutputTestDto create(OutputTest resources) {
        return outputTestMapper.toDto(outputTestRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OutputTest resources) {
        OutputTest outputTest = outputTestRepository.findById(resources.getId()).orElseGet(OutputTest::new);
        ValidationUtil.isNull( outputTest.getId(),"OutputTest","id",resources.getId());
        outputTest.copy(resources);
        outputTestRepository.save(outputTest);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            outputTestRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<OutputTestDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OutputTestDto outputTest : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", outputTest.getStudentName());
            map.put("年龄", outputTest.getAge());
            map.put("性别", outputTest.getGrade());
            map.put("创建时间", outputTest.getCreateDate());
            map.put("更新时间", outputTest.getUpdateDate());
            map.put("创建人", outputTest.getCreateUser());
            map.put("更新人", outputTest.getUpdateUser());
            map.put("格式化", outputTest.getFormat1());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}